// zhouyuyue_yajin/pages/placeOrder/placeOrder.js
let app = getApp();
let http = require('../../util/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // order_list:[{
    //   "format_week": "周三",
    //   "food": [{
    //     "id": "1",
    //     "name": "蔬菜1",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "11",
    //     "price": "21.50",
    //     "categoryName": "蔬菜",
    //     "count": 2, //购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }, {
    //     "id": "2",
    //     "name": "蔬菜2",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "22",
    //     "price": "12.00",
    //     "categoryName": "蔬菜",
    //     "count": 1 ,//购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }]
    // },{
    //   "format_week": "周二",
    //   "food": [{
    //     "id": "1",
    //     "name": "蔬菜1",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "11",
    //     "price": "21.50",
    //     "categoryName": "蔬菜",
    //     "count": 2 ,//购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }, {
    //     "id": "2",
    //     "name": "蔬菜2",
    //     "poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
    //     "sku": "22",
    //     "price": "12.00",
    //     "categoryName": "蔬菜",
    //     "count": 1 ,//购物车中的菜品数量
    //     "send_time":"2020-09-12"
    //   }]
    // }],//订单列表
    order_list:[],
    all_price:'0.00',//总价
    user_name:'',//用户名
    phone:'',//手机号
    role:''
  },

  //提交订单
  submit:function(){
    let that = this;
    wx.showModal({
      content: '是否确认提交订单？',
      showCancel: true,
      title: '提示',
      success: (result) => {
        if(result.confirm){
          http.post('cartToOrder',{
            type:app.globalData.role
          },function(res){
            console.log('提交订单',res);
            if(res.data.errno==0){
              wx.navigateTo({
                url: '/zhouyuyue_yajin/pages/submitted/submitted',
              })
            }
          });
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
    
  },

  //获取订单列表
  getOrderList:function(){
    let that = this;
    http.get('cartToOrder',{},function(res){
      console.log('订单列表',res);
      let all_price = 0;
      for(let i=0;i<res.data.data.list.length;i++){
        let item_price = 0;
        let item_num = 0;
        for(let j=0;j<res.data.data.list[i].food.length;j++){
          item_num = item_num + res.data.data.list[i].food[j].count*1;
          item_price = item_price + res.data.data.list[i].food[j].count*res.data.data.list[i].food[j].price;
        }
        all_price = all_price + item_price;
        res.data.data.list[i].all_num = item_num;
        res.data.data.list[i].all_price = item_price.toFixed(2);
      };
      that.setData({
        order_list:res.data.data.list,
        all_price:all_price.toFixed(2),
        user_name:res.data.data.realname,
        phone:res.data.data.phone
      });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getOrderList();
    that.setData({
      role:app.globalData.role==10?'老师':'学生'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})